package base;

class Node{
	  public int data;
	  public Node next;
	}

public class Linked {
	
	public Node head;
	
	public void insertAtEnd(int value){
		if(head==null){
			head = new Node();
			head.data = value;
		}else{
			Node temp = head;
			while(temp.next!=null){
				temp = temp.next;
			}
			Node newNode = new Node();
			newNode.data = value;
			temp.next = newNode;
		}
	}
	
	public void insertAtFront(int value){
		if(head == null){
			head = new Node();
			head.data = value;
		}else{
			Node temp = head;
			Node newNode = new Node();
			newNode.data = value;
			newNode.next = temp;
			head = newNode;
		}
	}
	
	public void deleteAtEnd(){
		if(head!=null){
			Node temp = head;
			while(temp.next.next!= null){
				temp = temp.next;
			}
			temp.next = null;
		}
	}
	public void display(){
		Node temp = head;
		while(temp.next!= null){
			System.out.print(temp.data+"->");
			temp = temp.next;
		}
		System.out.print(temp.data);
	}
	//1->2->3->4
	public Node reverse(Node node){
		Node temp = node;
		Node rev = null;
		if(temp.next!=null){
			rev = reverse(temp.next);
			rev.next = temp;
		}else{
			rev = temp;
		}
		node.next = null;
		return rev;
	}

	public static void main(String[] args) {
		Linked link = new Linked();
		link.insertAtEnd(1);
		link.insertAtEnd(2);
		link.insertAtEnd(3);
		//link.insertAtFront(4);
		//link.insertAtFront(5);
		//link.deleteAtEnd();
		//link.insertAtEnd(6);
		//link.insertAtFront(7);
		link.display();
		//link.head = link.reverse(link.head);
		
	}

}
